package com.zp.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import com.zp.api.ApiCode;
import com.zp.api.ApiResult;
import com.zp.config.MyCloudConfig;
import com.zp.config.ServerConfig;
import com.zp.util.CommonConstant;
import com.zp.util.HttpUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zp
 * @description 注册服务
 * @created 2021/11/24 13:43
 */

public class RegisterService {
    private static final Log log = LogFactory.getLog(RegisterService.class);

    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private MyCloudConfig myCloudConfig;
    @Autowired
    private RenewalService renewalService;

    /**
     * 客户端注册到注册中心
     */
    public void register() throws Exception {
        //1.获取地址和端口
        String hostAndPort = serverConfig.getHostAndPort();
        //2.获取服务名称
        String serviceName = myCloudConfig.getServiceName();
        //3.获取注册中心url
        String serviceRegisterUrl = myCloudConfig.getServiceRegisterUrl();
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put(CommonConstant.SERVICE_NAME,serviceName);
        paramMap.put(CommonConstant.SERVICE_ADDR_URL,hostAndPort);
        String result = HttpUtil.post("http://"+serviceRegisterUrl+CommonConstant.REGISTER_MAPPING_URL, JSON.toJSONString(paramMap));
        log.info("服务注册结果："+result);
        ApiResult apiResult = JSONObject.parseObject(result, ApiResult.class);
        if(apiResult.getCode() == ApiCode.SUCCESS.getCode()){
            log.info("注册成功，启动心跳续约程序...");
            renewalService.start();
        }
    }

    /**
     * 获取注册中心注册url
     * @return
     */
    public String activeService() throws Exception {
        String serviceRegisterUrl = myCloudConfig.getServiceRegisterUrl();
        String result = HttpUtil.get("http://"+serviceRegisterUrl+CommonConstant.ACTIVE_SERVICE_MAPPING_URL);
        log.info("获取注册中心活跃服务"+result);
        return result;
    }

    /**
     * 下线删除
     */
    public void remove() {
        //1.获取地址和端口
        String hostAndPort = serverConfig.getHostAndPort();
        //2.获取服务名称
        String serviceName = myCloudConfig.getServiceName();
        //3.获取注册中心url
        String serviceRegisterUrl = myCloudConfig.getServiceRegisterUrl();
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put(CommonConstant.SERVICE_NAME,serviceName);
        paramMap.put(CommonConstant.SERVICE_ADDR_URL,hostAndPort);
        try {
            String result = HttpUtil.post("http://"+serviceRegisterUrl+CommonConstant.REMOVE_MAPPING_URL, JSON.toJSONString(paramMap));
            log.info("当前客户端下线："+result);
        } catch (Exception e) {
            log.error(e);
        }
    }

    /**
     * 续约
     */
    public void renewal() throws Exception {
        //1.获取地址和端口
        String hostAndPort = serverConfig.getHostAndPort();
        //2.获取服务名称
        String serviceName = myCloudConfig.getServiceName();
        //3.获取注册中心url
        String serviceRegisterUrl = myCloudConfig.getServiceRegisterUrl();
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put(CommonConstant.SERVICE_NAME,serviceName);
        paramMap.put(CommonConstant.SERVICE_ADDR_URL,hostAndPort);
        String result = HttpUtil.post("http://"+serviceRegisterUrl+CommonConstant.RENEWAL_MAPPING_URL, JSON.toJSONString(paramMap));
        log.info("续约结果："+result);
    }
}
